import 'package:http/http.dart' as http;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:convert';

class GetOrderInfo extends StatelessWidget {
  final TextEditingController appKeyController = TextEditingController(text: 'mh_83c9393fcfd7b889');
  final TextEditingController vController = TextEditingController(text: '1.0');
  final TextEditingController signMethodController = TextEditingController(text: 'MD5');
  final TextEditingController signController = TextEditingController(text: '123456');
  final TextEditingController keywordController = TextEditingController(text: 'TESTWAYBILL');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('出口订单信息获取'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Column(
          children: <Widget>[
            TextField(
              controller: appKeyController,
              decoration: InputDecoration(hintText: 'mh_83c9393fcfd7b889'),
            ),
            TextField(
              controller: vController,
              decoration: InputDecoration(hintText: '1.0'),
            ),
            TextField(
              controller: signMethodController,
              decoration: InputDecoration(hintText: 'MD5'),
            ),
            TextField(
              controller: signController,
              decoration: InputDecoration(hintText: '123456'),
            ),
            TextField(
              controller: keywordController,
              decoration: InputDecoration(hintText: 'TESTWAYBILL'),
            ),

            ElevatedButton(
              child: Text('上传'),
              onPressed: () async {
                if (appKeyController.text.isEmpty ||
                    vController.text.isEmpty ||
                    signMethodController.text.isEmpty ||
                    signController.text.isEmpty||
                    keywordController.text.isEmpty) {
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text('错误'),
                        content: Text('请填写所有必要的信息。'),
                        actions: <Widget>[
                          TextButton(
                            child: Text('确定'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                        ],
                      );
                    },
                  );
                  return;
                }

                // 构建请求数据
                Map<String, dynamic> requestData = {
                  'app_key': appKeyController.text,
                  'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
                  'v': vController.text,
                  'sign_method': signMethodController.text,
                  'sign': signController.text,
                  'data': {
                    "keyword": keywordController.text,
                  }
                };

                // 发送请求
                http.Response response = await http.post(
                  Uri.parse('https://cbec-studapi.gzport.net/api/access_service/get_order_info'),
                  headers: <String, String>{
                    'Content-Type': 'application/json; charset=UTF-8',
                  },
                  body: jsonEncode(requestData),
                );

                if (response.statusCode == 200) {
                  print('上传成功');
                  Map<String, dynamic> responseData = jsonDecode(response.body);
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text('上传成功'),
                        content: SingleChildScrollView(
                          child: ListBody(
                            children: <Widget>[
                              Text('响应状态: ${responseData['status']}'),
                              Text('响应代码: ${responseData['code']}'),
                              Text('响应信息: ${responseData['message']}'),
                              Text('响应时间: ${responseData['time']}'),
                              Text('企业用户id: ${responseData['app_key']}'),
                              Text('签名算法: ${responseData['sign_method']}'),
                              Text('加密信息: ${responseData['sign']}'),
                              Text('响应数据: ${responseData['data']}'),
                            ],
                          ),
                        ),
                        actions: <Widget>[
                          TextButton(
                            child: Text('确定'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                        ],
                      );
                    },
                  );
                } else {
                  print('上传失败');
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text('上传失败'),
                        content: SingleChildScrollView(
                          child: ListBody(
                            children: <Widget>[
                              Text('响应状态: ${response.statusCode}'),
                              Text('响应信息: ${response.reasonPhrase}'),
                            ],
                          ),
                        ),
                        actions: <Widget>[
                          TextButton(
                            child: Text('确定'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                        ],
                      );
                    },
                  );
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}